package com.example.provider.controller;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.example.common.base.BaseController;
import com.example.common.base.Paging;
import com.example.common.base.Result;
import com.example.common.model.User;
import com.example.common.model.condition.UserCondition;
import com.example.provider.service.Impl.UserAttentionServiceImpl;
import com.example.provider.service.Impl.UserServiceImpl;
import com.example.provider.service.UserService;
import com.example.provider.type.DataType;
import com.example.provider.util.RedisUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;


/**
 * 用户信息表Controller
 * 
 * @author 510
 * @version 1.0.0 2021-04-23
 */
@Api(tags = "用户信息表服务")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserServiceImpl userServiceImpl;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserAttentionServiceImpl userAttentionServiceImpl;

    /**
     * 关注用户
     */
    @PostMapping("/attentionUser")
    public Boolean attentionUser(@RequestParam("userId") String userId, @RequestParam("beNoticedUserId") String beNoticedUserId){
        return userAttentionServiceImpl.attentionUser(userId, beNoticedUserId);
    }

    /**
     * 取消关注用户
     */
    @PostMapping("/reAttentionUser")
    public Boolean reAttentionUser(@RequestParam("userId") String userId, @RequestParam("beNoticedUserId") String beNoticedUserId){
        return userAttentionServiceImpl.reAttentionUser(userId, beNoticedUserId);
    }

    /**
     * 获取邮箱验证码
     */
    @PostMapping("/getCheckCode")
    public String getCheckCode(@RequestParam(name = "userId") String userId){
        //String userEmail = request.getParameter("userEmail");
        //@RequestParam Map<String, String> map
        //String userEmail = map.get("userEmail");
        System.out.println(userId);
        String checkCode = String.valueOf(new Random().nextInt(8999) + 1000);
        System.out.println(checkCode);
        String c = "success";
        String message = "您的注册码为：" + checkCode + " 10分钟有效时间。";
        try {
            userServiceImpl.sendSimpleMail(userId,"智校园验证码",message);
            redisUtil.set(DataType.CHECKCODE, userId, checkCode, 600);
        }catch (Exception e){
            redisUtil.del(DataType.CHECKCODE, userId);
            return "error";
        }
        return c;
    }

    /**
     * 注册验证
     */
    @PostMapping("/registered")
    public Map<String, Object> registered(
            @RequestParam(name = "userId") String userId,
            @RequestParam(name = "verificatioCode") String verificatioCode){
        System.out.println("user:::" + userId);
        System.out.println("verrrr" + verificatioCode);
        Map<String, Object> map = new HashMap<>();
        if (!userServiceImpl.checkCode(userId, verificatioCode)){
            map.put("error", "codeError");
            return map;
        }
        User user = new User();
        user.setUserId(userId);
        user.setUserAvatarUrl("/static/test/preview.jpg");
        user.setDeleteFlag(1);
        user.setUserName("新用户");
        user.setUserType("OrdinaryUsers");
        user.setCreationDate(new Date());
        user.setLastUpdateDate(new Date());
        user.setCreatedBy("administrator");
        user.setLastUpdatedBy("administrator");

        if (!userServiceImpl.addUser(user)){
            map.put("error", "registError");
            return map;
        }
        map.put("success", "codeTrue");
        map.put("userInfo", user);
        return map;
    }

    @ApiOperation(value = "根据查询参数分页查询用户信息表列表")
    @ApiImplicitParam(name = "condition", value = "用户信息表查询条件", required = true, dataType = "UserCondition", paramType = "body")
    @PostMapping("/list")
    public Paging<User> list(@RequestBody UserCondition condition) {
        IPage<User> page = userService.findUserByCondition(condition);
        return Paging.buildPaging(page);
    }

    @ApiOperation(value = "根据主键ID查询用户信息表信息")
    @ApiImplicitParam(name = "userId", value = "主键ID", required = true)
    @GetMapping(value = "/get/{userId}")
    public Result<User> get(@PathVariable String userId) {
        if (StringUtils.isBlank(userId)) {
            return Result.failed("请选择需要查询的数据！");
        }
        User user = userService.getUserById(userId);
        return Result.ok(user);
    }

    @ApiOperation(value = "新增用户信息表信息")
    @ApiImplicitParam(name = "user", value = "用户信息表", required = true, dataType = "User", paramType = "body")
    @PostMapping("/add")
    public Result<?> add(@RequestBody User user) {
        boolean bool = userService.addUser(user);
        if (bool) {
            return Result.ok(user);
        }
        return Result.failed();
    }

    @ApiOperation(value = "修改用户信息表信息")
    @ApiImplicitParam(name = "user", value = "用户信息表", required = true, dataType = "User", paramType = "body")
    @PutMapping(value = "/update")
    public Result<?> update(@RequestBody User user) {
        String userId = user.getUserId();
        if (StringUtils.isBlank(userId)) {
            return Result.failed("请选择需要修改的数据！");
        }
        boolean bool = userService.updateUser(user);
        return Result.okOrFailed(bool);
    }

    @ApiOperation(value = "根据主键ID删除用户信息表")
    @ApiImplicitParam(name = "userId", value = "主键ID", required = true)
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable String userId) {
        if (StringUtils.isBlank(userId)) {
            return Result.failed("请选择需要删除的数据！");
        }
        boolean bool = userService.deleteUserById(userId);
        return Result.okOrFailed(bool);
    }

    @ApiOperation(value = "根据主键ID列表批量删除用户信息表")
    @ApiImplicitParam(name = "idList", value = "主键ID列表", required = true, allowMultiple = true, paramType = "body")
    @DeleteMapping("/deleteList")
    public Result<?> deleteList(@RequestBody List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Result.failed("请选择需要删除的数据！");
        }
        boolean bool = userService.deleteUserByIds(idList);
        return Result.okOrFailed(bool);
    }
}